package com.example.springboottest.example.refrect;

import cn.hutool.core.util.StrUtil;
import com.example.springboottest.student.model.Student;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

public class IntrospectorTest {
    public static void main(String[] args) {
        try {
            Student student = Student.class.newInstance();
            System.out.println(student.toString());
            BeanInfo studentInfo = Introspector.getBeanInfo(Student.class);

            PropertyDescriptor[] propertyDescriptors = studentInfo.getPropertyDescriptors();
            for (PropertyDescriptor pro : propertyDescriptors) {
                Method method = pro.getWriteMethod();
                if (method != null && StrUtil.equals(method.getName(), "setName"))
                    method.invoke(student, "方宁");
            }
            for (PropertyDescriptor pro : propertyDescriptors) {
                Method method = pro.getReadMethod();
                Object object = method.invoke(student);
                System.out.println(method.getName() + "\t" + (object != null ? object.toString() : ""));
            }

//            Student.class.getde
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
